package com.vectz.mgt.action;

import java.io.IOException;
import java.util.ArrayList;

import com.vectz.mgt.model.Entity;
import com.vectz.mgt.model.EntityOverview;
import com.vectz.mgt.util.Constant;
import com.vectz.mgt.util.Log;
import com.vectz.mgt.util.Util;

/**
 * @author ai-t.vn
 * 
 */
public class XmlPageCodegenAction {
	ArrayList<Entity> listField=null;
	EntityOverview table = null;
	String outputFolder = "";
	public XmlPageCodegenAction(ArrayList<Entity> listField, EntityOverview table, 
			String outputFolder) throws Exception{
		this.listField = listField;
		this.table = table;
		this.outputFolder = outputFolder;
		genernateXmlFile();
	}

	/*
	 * Generate xml file
	 */
	public void genernateXmlFile() throws Exception {
			String tmplFormContent = Util.readTextFileContent(Util.getTmplPath("xml", "PageXml.txt"));
			
			if(table.getDestScreenPath().lastIndexOf("\\")!= -1)
				tmplFormContent = tmplFormContent.replaceAll("xxxxWebDir",
						"/" + Util.getWebbaseDir(table.getDestScreenPath()));
			else
				tmplFormContent = tmplFormContent.replaceAll("xxxxWebDir","");
			
			tmplFormContent = tmplFormContent.replaceAll("xxxxPage",
					Util.lowerCaseFirstChar(table.getName()));
			
			tmplFormContent = tmplFormContent.replaceAll(
					"xxxxInitList", Util.buildButtonAction(table.getName(), "initList"));
			
			tmplFormContent = tmplFormContent.replaceAll(
					"xxxxInitNew", Util.buildButtonAction(table.getName(), "initNew"));

			tmplFormContent = tmplFormContent.replaceAll(
					"xxxxInitDelete", Util.buildButtonAction(table.getName(), "delete"));
			
			tmplFormContent = tmplFormContent.replaceAll("xxxxInitModify",Util.buildButtonAction(table.getName(), "initModify"));
			tmplFormContent = tmplFormContent.replaceAll("xxxxEntityId",getIdField(listField));
			// add 2008-10-17
			// initModify in result page
			tmplFormContent = tmplFormContent.replaceAll("xxxxModifyEntityId",new StringBuffer()
					.append(Util.lowerCaseFirstChar(table.getName()))
					.append("Action.")
					.append(getIdField(listField)).toString());
			
			tmplFormContent = tmplFormContent.replaceAll(
					"xxxxConfirmAction", Util.buildButtonAction(table.getName(), "confirm"));
			
			tmplFormContent = tmplFormContent.replaceAll(
					"xxxxInitDownload", Util.buildButtonAction(table.getName(), "download"));
			
			tmplFormContent = tmplFormContent.replaceAll(
					"xxxxBack", Util.buildButtonAction(table.getName(), "back"));
			
			tmplFormContent = tmplFormContent.replaceAll("xxxxBackDelete", Util
					.buildButtonAction(table.getName(), "backDelete"));
			
			tmplFormContent = tmplFormContent.replaceAll(
					"xxxxPersist", Util.buildButtonAction(table.getName(), "persist"));
			
			tmplFormContent = tmplFormContent.replaceAll(
					"xxxxResultPage", "result"+table.getName());
			
			tmplFormContent = tmplFormContent.replaceAll(
					"xxxxConfirmPage", "confirm"+table.getName());
			
			tmplFormContent = tmplFormContent.replaceAll(
					"xxxxDownload", Util.lowerCaseFirstChar(table.getName())+"Download");
			
			String fileLocation = new StringBuffer().append(outputFolder)
			.append(System.getProperty("file.separator"))
			.append("WebContent")
			.append(System.getProperty("file.separator"))
			.append("WEB-INF")
			.append(System.getProperty("file.separator"))
			.append("page-")
			.append(Util.lowerCaseFirstChar(table.getName()))
			.append(".xml").toString();
			
			Util.writeJavaFile(fileLocation,tmplFormContent);
			Log.log("Finish generate xml file to:"+ fileLocation);
	}

	/*
	 * Generate Id field in model class
	 */
	public String getIdField(ArrayList<Entity> listField){
		if (listField == null && listField.size() == 0)
			return "";
		Entity field = null;
		for (int i = 0; i < listField.size(); i++) {
			field = (Entity) listField.get(i);
			if(field.getKeyType().equalsIgnoreCase("PK")){
				return new StringBuffer()
//				.append(Util.lowerCaseFirstChar(table.getName()))
//				.append("Action.")
				.append(Util.lowerCaseFirstChar(table.getName()))
				.append(".")
				.append(field.getName()).toString();
			}
		}
		return "";
	}
}
